﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// index 的摘要说明
    /// </summary>
    public class index : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"company\":\"日照创诚\",");
            //获取求购信息
            BLL.qiugou bll = new BLL.qiugou();
            DataSet ds = bll.GetList(10, "status=1", "add_time desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"qiugou\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");
                    if (string.IsNullOrEmpty(dr["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    strTxt.Append(",\"content\":\"" + Microsoft.JScript.GlobalObject.escape(dr["content"].ToString().Replace("<p>", "").Replace("</p>", "")) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取供应信息
            BLL.gongying bll5 = new BLL.gongying();
            DataSet ds5 = bll5.GetList(10, "status=1", "add_time desc");
            //如果记录存在
            if (ds5.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"gongying\":[");
                for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
                {
                    DataRow dr5 = ds5.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr5["id"]);
                    strTxt.Append(",\"title\":\"" + dr5["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr5["size_long"] + "×" + dr5["size_wide"] + "×" + dr5["size_height"] + "\"");
                    if (string.IsNullOrEmpty(dr5["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr5["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    string remark = dr5["content"].ToString().Replace("<p>", "").Replace("</p>", "");
                    if (remark.Length > 6)
                    {
                        remark = remark.Substring(0,6);
                    }

                    strTxt.Append(",\"content\":\"" + Microsoft.JScript.GlobalObject.escape(remark) + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr5["add_time"])) + "\"");
                    strTxt.Append(",\"market_price\":\"" + dr5["price"] + "/" + dr5["unit"] + "\"");
                    strTxt.Append(",\"mobile\":\"" + dr5["mobile"] + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr5["lianxiren"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds5.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取价格行情
            BLL.hangqing_price bll_h = new BLL.hangqing_price();
            DataSet ds_h = bll_h.GetLists(10,"");
            //如果记录存在
            if (ds_h.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"hangqing_list\":[");
                for (int i = 0; i < ds_h.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_h = ds_h.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr_h["id"]);
                    string shicainame = new BLL.hangqing().GetTitle(int.Parse(dr_h["article_id"].ToString()));
                    string _title = dr_h["title"].ToString();
                    if (_title.Length > 7)
                    {
                        strTxt.Append(",\"title\":\"" + shicainame + " " + _title.Substring(0, 7) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"title\":\"" + shicainame + " " + _title + "\"");
                    }
                    strTxt.Append(",\"shicainame\":\"" + shicainame + "\"");
                    strTxt.Append(",\"price\":\"" + dr_h["price"] + "\"");
                    strTxt.Append(",\"is_up\":" + dr_h["is_up"]);
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds_h.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取询价
            BLL.xunjia bll_x = new BLL.xunjia();
            DataSet ds_x = bll_x.GetList(5, "status=0", "id desc");
            //如果记录存在
            if (ds_x.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"xunjia_list\":[");
                for (int i = 0; i < ds_x.Tables[0].Rows.Count; i++)
                {
                    DataRow dr_x = ds_x.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr_x["id"]);
                    string _remark = Microsoft.JScript.GlobalObject.escape(dr_x["remark"].ToString());
                    strTxt.Append(",\"remark\":\"" + _remark + "\"");
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr_x["add_time"])) + "\"");
                    int _user_id = int.Parse(dr_x["user_id"].ToString());
                    string _user_name = new BLL.users().GetUserName(_user_id);
                    string _avatar = new BLL.users().GetAvatar(_user_id);
                    strTxt.Append(",\"user_name\":\"" + _user_name + "\"");

                    if (string.IsNullOrEmpty(_avatar))
                    {
                        strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                    }
                    else if (_avatar.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                    }
                    strTxt.Append(",\"hits\":" + dr_x["hits"]);
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds_x.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取供应信息
            BLL.index_company bll2 = new BLL.index_company();
            DataSet ds2 = bll2.GetList(9, "", "sort_id asc,id desc");
            //如果记录存在
            if (ds2.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"index_company\":[");
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr2 = ds2.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"company_id\":" + dr2["company_id"]);
                    strTxt.Append(",\"title\":\"" + dr2["title"] + "\"");
                    string picurl = dr2["img_url"].ToString();
                    if (picurl.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"" + picurl + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                    }
                    string _mobile = new BLL.company().GetMobile(Convert.ToInt32(dr2["company_id"]));
                    strTxt.Append(",\"mobile\":\"" + _mobile + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds2.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            //获取新闻信息
            BLL.news bll3 = new BLL.news();
            //DataSet ds3 = bll3.GetList(15, "status=1", "id desc");
            DataSet ds3 = bll3.GetList(15, 1, "", "id desc", out this.totalCount);
            int total_page = this.totalCount / 15;
            strTxt.Append("\"total_page\":" + total_page.ToString()+",");

            //如果记录存在
            if (ds3.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"news_list\":[");
                for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
                {
                    DataRow dr3 = ds3.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr3["id"]);
                    strTxt.Append(",\"title\":\"" + dr3["title"] + "\"");
                    strTxt.Append(",\"description\":\"" + Microsoft.JScript.GlobalObject.escape(dr3["description"].ToString()) + "\"");
                    if (string.IsNullOrEmpty(dr3["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr3["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds3.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }

            //获取产品信息
            BLL.product bll4 = new BLL.product();
            DataSet ds4 = bll4.GetList(9, "status=1", "add_time desc");
            //如果记录存在
            if (ds4.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"product_list\":[");
                for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
                {
                    DataRow dr4 = ds4.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr4["id"]);
                    if (dr4["title"].ToString().Length > 7)
                    {
                        strTxt.Append(",\"title\":\"" + dr4["title"].ToString().Substring(0,7) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"title\":\"" + dr4["title"] + "\"");
                    }
                    
                    if (Convert.ToInt32(dr4["price"]) > 0)
                    {
                        strTxt.Append(",\"market_price\":\"" + dr4["price"] + "元/" + dr4["unit"] + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"market_price\":\"面议\"");
                    }
                    string picurl = dr4["img_url"].ToString();
                    if (string.IsNullOrEmpty(picurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else if (picurl.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                    }
                    if (Convert.ToInt32(dr4["company_id"]) > 0)
                    {
                        strTxt.Append(",\"company\":\"" + new BLL.company().GetTitle(Convert.ToInt32(dr4["company_id"])) + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds4.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}